package com.pai4j.aigc.consumer.redis;


import com.pai4j.aigc.llm.AiAgentFlowService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class RobotChatRedisQueueListener implements MessageListener {

    private final StringRedisSerializer stringSerializer = new StringRedisSerializer();

    @Autowired
    private AiAgentFlowService aiAgentFlowService;

    @Override
    public void onMessage(Message message, byte[] pattern) {
        try {
            String channel = stringSerializer.deserialize(message.getChannel());
            String messageStr = stringSerializer.deserialize(message.getBody());
            log.info("RobotChatRedisQueueListener.onMessage =====> channel:{} messageStr:{}", channel, messageStr);

            aiAgentFlowService.doAgent(messageStr);
        } catch (Exception e) {
            log.error("Error processing Redis message in RobotChatRedisQueueListener", e);
        }
    }
}
